package main.java.indi.zyj.coderecord;

import java.util.Stack;

public class DailyTemperatures {


    public int[] dailyTemperatures(int[] temperatures) {

        int len = temperatures.length;
        if (len == 0 || temperatures == null) {
            return new int[]{};
        }

        Stack<Integer> stack = new Stack<>();
        int[] res = new int[len];

        for (int i = 0; i < len; i++) {
            while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                Integer index = stack.pop();
                res[index] = i - index;
            }
            stack.add(i);
        }

        return res;

    }


    public int[] dailyTemperatures1(int[] temperatures) {

        if (temperatures.length == 0 || temperatures == null) {
            return new int[]{};
        }

        Stack<Integer> stack = new Stack<>();
        int len = temperatures.length;
        int[] res = new int[len];

        for (int i = 0; i < len; i++) {
            while (!stack.isEmpty() && temperatures[i] > temperatures[stack.peek()]) {
                Integer index = stack.pop();
                res[index] = i - index;
            }
            stack.add(i);
        }

        return res;

    }

}
